declare const sharedCanvas;
declare const wx;

class WxUtils {
  isWechat = false;

  constructor() {
    this.isWechat = cc.sys.platform === cc.sys.WECHAT_GAME;
  }

  getSharedCanvas() {
    if (!this.isWechat) {
      return {};
    }
    return sharedCanvas;
  }

  postMessage(data: any) {
    if (!this.isWechat) {
      return;
    }
    wx.postMessage(data)
  }

  showLoading(data: any) {
    if (!this.isWechat) {
      return;
    }
    wx.showLoading(data);
  }

  hideLoading() {
    if (!this.isWechat) {
      return;
    }
    wx.hideLoading();
  }

  setBootstrap() {
    if (this.isWechat) {
      wx.setStorage({
        key: "bootstrap",
        data: "1"
      })
    } else {
      localStorage.setItem("bootstrap", "1")
    }
  }

  getBootstrapSync() {
    if (this.isWechat) {
      return wx.getStorageSync("bootstrap")
    } else {
      return localStorage.getItem("bootstrap")
    }
  }
}

export const wxUtils = new WxUtils();